package com.unionpay.upyzt.example;

import com.unionpay.upyzt.exception.UpyztException;
import com.unionpay.upyzt.model.CombinedDeposit;
import com.unionpay.upyzt.resp.CombinedDepositResp;

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yf.zhao
 * @version 1.0
 * @since 2020/10/29
 */
public class CombinedDepositExample {

    private String balanceAcctId;

    public CombinedDepositExample(String balanceAcctId) {
        this.balanceAcctId = balanceAcctId;
    }

    /**
     * 运行 Demos
     *
     * @param balanceAcctId 电子账簿ID
     */
    public static void runDemos(String balanceAcctId) throws UpyztException {
        CombinedDepositExample combinedDepositExample = new CombinedDepositExample(balanceAcctId);

        // 合并支付充值
        CombinedDepositResp combinedDepositResp = combinedDepositExample.create();

        // 合并支付充值订单查询（系统订单号）
        combinedDepositResp = combinedDepositExample.retrieveById(combinedDepositResp.getCombinedDepositId());

        // 合并支付充值订单查询（平台订单号）
        combinedDepositResp = combinedDepositExample.retrieveByOutOrderNo(combinedDepositResp.getOutOrderNo());

        DepositExample depositExample = new DepositExample(balanceAcctId);
        depositExample.retrieveById(combinedDepositResp.getDepositResults().get(0).getDepositId());
        depositExample.retrieveByOutOrderNo(combinedDepositResp.getDepositResults().get(0).getOutOrderNo());
    }

    /**
     * 合并支付充值
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public CombinedDepositResp create() throws UpyztException {
        Map<String, Object> params = new HashMap<>();
        params.put("combined_out_order_no", Main.generateRandomOutNo());
        params.put("sent_at", OffsetDateTime.now().toString());
        params.put("deposit_type", "1");
        params.put("payment_type", "SDK_TEST");
        params.put("payment_trade_no", Main.generateRandomOutNo());
        params.put("payment_succeeded_at", OffsetDateTime.now().toString());
        List<Map<String, Object>> depositParams = new ArrayList<>(1);
        Map<String, Object> depositParam = new HashMap<>();
        depositParam.put("amount", 100);
        depositParam.put("balance_acct_id", this.balanceAcctId);
        depositParam.put("out_order_no", Main.generateRandomOutNo());
        depositParam.put("remark", "用于 SDK 示例测试");
        depositParam.put("metadata", MockData.metadata());
        depositParam.put("extra", MockData.extra());
        depositParams.add(depositParam);
        params.put("deposit_params", depositParams);
        System.out.println("------- 合并支付充值 -------");
        CombinedDepositResp resp = CombinedDeposit.create(params);
        System.out.println(resp);
        return resp;
    }


    /**
     * 合并支付充值订单查询（系统订单号）
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public CombinedDepositResp retrieveById(String id) throws UpyztException {
        System.out.println("------- 合并支付充值订单查询（系统订单号） -------");
        CombinedDepositResp resp = CombinedDeposit.retrieveById(id);
        System.out.println(resp);
        return resp;
    }

    /**
     * 合并支付充值订单查询（平台订单号）
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public CombinedDepositResp retrieveByOutOrderNo(String outOrderNo) throws UpyztException {
        System.out.println("------- 合并支付充值订单查询（平台订单号） -------");
        CombinedDepositResp resp = CombinedDeposit.retrieveByOutOrderNo(outOrderNo);
        System.out.println(resp);
        return resp;
    }

}
